<template>
	<div>
		<el-form ref="form" :model="form" :rules="formRules" label-width="100px">
			<el-form-item label="名称" prop="wasteName">
				<el-autocomplete style="width: 250px" popper-class="wasteName-autocomplete" v-model="form.wasteName"
					:fetch-suggestions="querySearchAsync" @select="handleSelect">
					<template slot-scope="props">
						<div class="name">{{ props.item.wasteName }}</div>
					</template>
				</el-autocomplete>
			</el-form-item>
			<el-form-item label="代码" prop="wasteCode">
				<el-input v-model="form.wasteCode" style="width: 250px;"></el-input>
			</el-form-item>
			<el-form-item label="计量单位" prop="unitCode">
				<el-select v-model="form.unitCode" style="width: 250px;">
					<el-option :label="item.value" :value="item.code" v-for="(item,index) in unitList">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="类型" prop="gasType">
				<el-radio-group v-model="form.gasType" @change="againSelect">
					<el-radio label="危险废物">危险废物</el-radio>
					<el-radio label="一般工业固废">一般工业固废</el-radio>
					<el-radio label="其他">其他</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item style="text-align: center;margin-left: -90px!important;">
				<el-button v-if="editData && editData.id" type="danger" @click="deleteThis">删除</el-button>
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	let that
	export default {
		name: 'addStock',
		components: {},
		props: ['outletId', 'editData'],
		data() {
			return {
				params: {
					facilitatorId: '',
					categoryMainId: '',
					ticketId: '',
					itemId: '',
					subjectId: '',
				},
				form: {
					sideProductId: '',
					outletId: '',
					wasteName: '',
					wasteCode: '',
					gasType: '危险废物',
					sideProductType: 'HW_WASTE'
				},

				formRules: {
					wasteName: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
					wasteCode: [{
						required: true,
						message: '请输入编码',
						trigger: 'blur'
					}],
				},
				wasteList: [],
				unitList: []
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.facilitatorId = that.getQueryString("facilitatorId")
			that.params.categoryMainId = that.getQueryString("categoryMainId")
			that.params.ticketId = that.$store.state.token
			that.params.itemId = that.getQueryString("itemId")
			that.params.subjectId = that.getQueryString("subjectId")

			if (that.editData) that.form = that.editData
			that.form.outletId = that.outletId

			that.getUnitList();
			that.getWasteList()
		},
		methods: {
			cancle() {
				that.$emit("cancle", true)
			},

			getUnitList: function() {
				var _this = this;
				that.ajax({
					url: '/codeType/listCodeValue',
					data: {
						typeCode: 'UNIT_TYPE'
					},
					success: function(data) {
						if (data.status == 1) {
							_this.unitList = data.data;
						}
					}
				});
			},

			querySearchAsync(queryString, cb) {
				var wasteList = this.wasteList;
				var results = queryString ? wasteList.filter(this.createStateFilter(queryString)) : wasteList;
				setTimeout(function() {
					cb(results);
				}, 1000)
			},

			createStateFilter(queryString) {
				return (state) => {
					return (state.wasteName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
				};
			},

			handleSelect(item) {
				that.form.wasteName = item.wasteName
				that.form.wasteCode = item.wasteCode
				that.form.sideProductId = item.id
			},

			againSelect() {
				that.form.wasteName = ""
				that.form.wasteCode = ""
				that.form.sideProductId = ''
				that.getWasteList
			},

			getWasteList: function() {
				if (that.form.gasType == "危险废物") {
					that.form.sideProductType = "HW_WASTE"
					that.ajax({
						url: '/areaSubject/getFacilitatorCustomerDetailInfo?ticketId=' + that.params.ticketId,
						data: {
							facilitatorId: that.params.facilitatorId,
							subjectId: that.params.subjectId,
							ticketId: that.params.ticketId,
						},
						//contentType: 'application/json',
						method: 'POST',
						success: function(result) {
							if (result.data) {
								that.params.entId = result.data.entId
								var param = {
									entId: that.params.entId
								}
								that.ajax({
									url: '/entWaste/getEntWasteInfoByEntId.htm?ticketId=' + that
										.params.ticketId,
									dataType: 'json',
									data: param,
									success: function(result) {
										if (result.status == 1) {
											that.wasteList = result.data
										}
									}
								})
							}
						},
					})
				} else if (that.form.gasType == "一般工业固废") {
					that.form.sideProductType = "SW_WASTE"
					var param = {
						facilitatorId: that.params.facilitatorId
					}
					that.ajax({
						url: '/sw/entWaste/getSwEntWasteModelList?ticketId=' + that.params.ticketId,
						contentType: 'application/json',
						data: JSON.stringify(param),
						success: function(result) {
							if (result.status == 1) {
								that.wasteList = result.data
							}
						}
					})
				} else {
					that.form.sideProductType = "SIDE_PRODUCT"
					that.ajax({
						url: '/entSideProduct/getEntSideProductList?ticketId=' + that.params.ticketId,
						contentType: 'application/json',
						data: JSON.stringify({
							facilitatorId: that.params.facilitatorId
						}),
						success: function(result) {
							if (result.status == 1) {
								that.wasteList = result.data
							}
						}
					})
				}

			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/gasOutlet/deleteGasOutletPollutione?ticketId=' + that.params.ticketId,
						data: {
							id: that.editData.id
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.status && result.data) {
								that.data = that.$options.data()
								that.$forceUpdate()
								that.$emit("success", true)
							}
						}
					})
				})
			},

			saveOrUpdate: function() {
				var _this = this;
				this.$refs['form'].validate(function(valid) {
					if (valid) {
						_this.form.facilitatorId = that.params.facilitatorId
						that.ajax({
							url: '/gasOutlet/saveOrUpdateGasOutletSideProduct?ticketId=' + that.params
								.ticketId,
							data: JSON.stringify(_this.form),
							contentType: 'application/json',
							success: function(result) {
								if (result.status == 1 && result.data) {
									that.data = that.$options.data()
									that.$forceUpdate()
									if (_this.form.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
								} else {}
							}
						});
					}
				});
			},

		}
	}
</script>

<style>

</style>
